package com.qs.rocketmq.mb.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.Serializable;

/**
 * 结果对象
 */
public class ResultObject<T> implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(ResultObject.class);

    private boolean success = true;

    private long errorCode;

    private String message;

    private T result;


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public long getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(long errorCode) {
        this.errorCode = errorCode;
        this.setSuccess(false);
    }

    public void setErrorCodeWithTrans(long errorCode) {
        this.errorCode = errorCode;
        this.setSuccess(false);
        try {
            TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();
            status.setRollbackOnly();
            logger.info("事务回滚成功");
        } catch (NoTransactionException e) {
            logger.debug("当前方法没有事务控制");
        }
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }
}
